package geral.manager;

import geral.util.FacesUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;

import br.com.sgc.entities.geral.Exame;
import br.com.sgc.session.GenericSession;

@Named
@ConversationScoped
public class ExameManager extends GenericManager implements Serializable{
	
	private static final long serialVersionUID = -439692882151383136L;

	@Inject private UtilManager utilManager;
	
	@EJB private GenericSession<Exame> exameSession;
	
	private Exame exame = new Exame();
	
	private List<Exame> exames = new ArrayList<Exame>();
	
	private Boolean showPanelCadastro;

	
	public String list(){
		exames = exameSession.findAll(Exame.class, utilManager.getSegurancaManager().getUsuarioLogado().getClinica().getId());
		showPanelCadastro = false;
		return "formExame";
	}
	
	public String prepareSave(){
		showPanelCadastro = true;
		exame = new Exame();
		return "formExame";
	}
	
	public String save(){
		try {
			exame.setClinica(utilManager.getSegurancaManager().getUsuarioLogado().getClinica());
			exameSession.save(exame);
			FacesUtil.mensInfo("Exame salvo com sucesso");
			exame = new Exame();
			return list();
		} catch (Exception e) {
			e.printStackTrace();
			FacesUtil.mensErro(e.getMessage());
			return null;
		}
	}
	
	public String prepareUpdate(){
		exame = exameSession.findById(Exame.class, exame.getId(), true);
		showPanelCadastro = true;
		return "formExame";
	}
	
	public String update(){
		try {
			exame.setClinica(utilManager.getSegurancaManager().getUsuarioLogado().getClinica());
			exameSession.update(exame);
			exame = new Exame();
			FacesUtil.mensInfo("Exame atualizado com sucesso");
			return list();
		} catch (Exception e) {
			FacesUtil.mensErro(e.getMessage());
			return null;
		}
	}

	public GenericSession<Exame> getExameSession() {
		return exameSession;
	}

	public void setExameSession(GenericSession<Exame> exameSession) {
		this.exameSession = exameSession;
	}

	public Exame getExame() {
		return exame;
	}

	public void setExame(Exame exame) {
		this.exame = exame;
	}

	public List<Exame> getExames() {
		return exames;
	}

	public void setExames(List<Exame> exames) {
		this.exames = exames;
	}

	public Boolean getShowPanelCadastro() {
		return showPanelCadastro;
	}

	public void setShowPanelCadastro(Boolean showPanelCadastro) {
		this.showPanelCadastro = showPanelCadastro;
	}

	
}
